package cn.liwq.demo.java.raw.ver8.lambda;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class LambdaDemo {
    public static void main(String[] args) {
        lambdaRunnable();
        lambdaIterator();
    }

    /**
     * 用lambda表达式实现Runnable
     */
    private static void lambdaRunnable() {
        // java 8 之前
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Before Java8, too much code for too little to do");
            }
        }).start();
        // java 8
        new Thread(() -> System.out.println("In Java8, one line code is enough.")).start();
    }

    /**
     * 用lambda表达式进行迭代
     */
    private static void lambdaIterator() {
        // Java 8之前
        List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        for (String feature : features) {
            System.out.println(feature);
        }
        // Java 8之后：
        List features2 = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        features2.forEach(n -> System.out.println(n));

        // 使用Java 8的方法引用更方便，方法引用由::双冒号操作符标示，
        // 看起来像C++的作用域解析运算符
        features2.forEach(System.out::println);
    }
}
